$(function(){
	
	$(document).on('change','select.itembank-select',function(){
		var value = parseInt($(this).val());
		var params = {
				url: '/admin/enterprise/exams/nums/'+value,
				type: 'GET',
				dataType: 'json',
				data:{},
		};
		
		var container = $('#questions-container');
        var questionTemplate = _.template($('#question-list-template').html());
        
		$.ajax(params).done(function(data){
			container.html(questionTemplate({item:data.data}));
			inintIcheck();
		}).fail(function(erro){
			error(erro);
		})
	});
	
	$(document).on('submit','.examinationForm',function(){
		var count_ques = form.find('input[name=question_count]').val();
		var count_score = form.find('input[name=score_count]').val();
		if(isEmpty(count_ques)){
			layer.open({
				title:'错误消息',
				content:'题目数不能为空'
			});
			return false;
		} 
		if(isEmpty(count_score)){
			layer.open({
				title:'错误消息',
				content:'分值不能为空'
			});
			return false;
		} 
		var pass_prop = form.find('input[name=pass_prop]').val();
		if(isEmpty(pass_prop)){
			layer.open({
				title:'错误消息',
				content:'合格比不能为空'
			});
			return false;
		}
		var times = form.find('input[name=times]').val();
		var data = {
			name:getFormValue('name'),
			question_count:count_ques,
			score_count:count_score,
			pass_prop:pass_prop,
			itembank_id:$('#examinationForm').find('select[name=itembank_id]').val(),
			radio_num:getFormValue('radio_num'),
			mulit_num:getFormValue('mulit_num'),
			judge_num:getFormValue('judge_num'),
			discuss_num:getFormValue('discuss_num'),
			radio_score:getFormValue('radio_score'),
			mulit_score:getFormValue('mulit_score'),
			judge_score:getFormValue('judge_score'),
			discuss_score:getFormValue('discuss_score'),
			times:times
		}
		var params = {
				url: '/admin/enterprise/exams/auto-create',
				type: 'POST',
				dataType: 'json',
				data:data,
		};
		
		$.ajax(params).done(function(resp){
			if(resp.status=='success') {
				BootstrapDialog.alert({title:'提示',message:resp.msg,callback: function(result) {
                    window.location.href= resp.url;
	            }});
			} else {
				BootstrapDialog.alert({title:'提示',message:resp.msg});
			}
		}).fail(function(erro){
			error(erro);
		});
		return false;
	});
	
	var form = $('#examinationForm');
	
	form.on('change',saveAutoForm);
	
	function getFormValue(id){
		return form.find('input[name='+id+']').val();
	}
	
	function isEmpty(data){
		if(isNaN(data) || typeof(data) == 'undefined' || data =='' || data == '0' || data == 0){
			return true;
		} 
		return false;
	}
	
	function saveAutoForm(){
		var radio_num = isNone('radio_num');
		var radio_score = isNone('radio_score');
		var mulit_num = isNone('mulit_num');
		var mulit_score = isNone('mulit_score');
		var judge_num = isNone('judge_num');
		var judge_score = isNone('judge_score');
		var discuss_num = isNone('discuss_num');
		var discuss_score = isNone('discuss_score');
		
		form.find('input[name=question_count]').val(radio_num+mulit_num+judge_num+discuss_num);
		form.find('input[name=score_count]').val(
				(radio_score*radio_num)+(mulit_score*mulit_num)+(judge_score*judge_num)+(discuss_score*discuss_num)
				);
	}
	
	function isNone(attr){
		var data = parseInt(form.find('input[name='+attr+']').val());
		var maxNum = parseInt($('#'+attr).text());
		
		if(!isNaN(data) && typeof(data) != 'undefined' && data != '') {
			if(data > maxNum) {
				form.find('input[name='+attr+']').val(maxNum);
				return maxNum;
			}
			return data;
		} else {
			return 0;
		}
	}
})